﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QtGui>

#include "glwidget.h"
#include <stdlib.h>

extern QString butterfly_fn;
extern QString logo_fn;


class ImageItem: public QGraphicsRectItem
{
public:
    ImageItem( QImage *img );
protected:
    void paint( QPainter *, const QStyleOptionGraphicsItem *option, QWidget *widget );
private:
    QImage image;
    QPixmap pixmap;
};


ImageItem::ImageItem( QImage *img )
    : image(*img)
{
    setRect(0, 0, image.width(), image.height());
    setFlag(ItemIsMovable);
#if !defined(Q_WS_QWS)
    pixmap.fromImage(image, Qt::OrderedAlphaDither);
#endif
    int k = 0;
    k = 1;
}

void ImageItem::paint( QPainter *p, const QStyleOptionGraphicsItem *option, QWidget * )
{
// On Qt/Embedded, we can paint a QImage as fast as a QPixmap,
// but on other platforms, we need to use a QPixmap.
//#if defined(Q_WS_QWS)
    p->drawImage( option->exposedRect, image, option->exposedRect, Qt::OrderedAlphaDither );
//#else
 //   p->drawPixmap( option->exposedRect, pixmap, option->exposedRect );
//#endif
}




MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    //printf("=======================");


    //QGridLayout *
    mainLayout = new QGridLayout;
    QGridLayout *mainLayout_2 = new QGridLayout;
    for (int i = 0; i < NumRows; ++i) {
        for (int j = 0; j < NumColumns; ++j) {
            QColor clearColor;
            clearColor.setHsv((255),
                              255, 63, 50);

            glWidgets[i][j] = new GLWidget(0, 0);
            glWidgets[i][j]->setClearColor(clearColor);
            //glWidgets[i][j]->rotateBy(+42 * 16, +42 * 16, -21 * 16);
            mainLayout->addWidget(glWidgets[i][j], i, j);

            connect(glWidgets[i][j], SIGNAL(clicked()),
                    this, SLOT(setCurrentGlWidget()));
        }
    }

    for (int i = 0; i < NumRows; ++i) {
        for (int j = 0; j < NumColumns; ++j) {
            QColor clearColor;
            clearColor.setHsv(((j * NumColumns) + i) * 255
                              / (NumRows * NumColumns ),
                              255, 0);

            //glWidgets[i][j] = new GLWidget(0, 0);
            //glWidgets[i][j]->setClearColor(clearColor);
            //glWidgets[i][j]->rotateBy(+42 * 16, +42 * 16, -21 * 16);
            //mainLayout_2->addWidget(glWidgets[i][j], i, j);

            //connect(glWidgets[i][j], SIGNAL(clicked()),
            //        this, SLOT(setCurrentGlWidget()));
        }
    }

    //mainLayout_2->addWidget(glWidgets[0][0], 0, 0);
    //setLayout(mainLayout);
    ui->setupUi(this);
    ui->frame_2->setLayout(mainLayout_2);
    ui->dockWidgetContents->setLayout(mainLayout);
   // ui->tableView->addScrollBarWidget((QWidget*)glWidgets[0][0], (Qt::Alignment)0x0100);
    currentGlWidget = glWidgets[0][0];
    //ui->verticalSlider->get
    //QTimer *timer = new QTimer(ui->centralWidget);
    //connect(timer, SIGNAL(timeout()), this, SLOT(rotateOneStep()));
    //timer->start(20);

    QTimer timer;
    QObject::connect(&timer, SIGNAL(timeout()), this, SLOT(rotateOneStep()));
    timer.start(30);

    setWindowTitle(tr("Test00"));
    //ui->dockWidgetContents_2->setLayout(mainLayout_2);

 //=====================================================================

    //QGraphicsScene can;
    //can.setSceneRect(0, 0, 800, 600);

    //setCentralWidget(editor);
    //mainLayout_2->addWidget(editor, 0, 1);
    // 리스트 레이아웃
    QListWidgetItem *gli = new QListWidgetItem(tr("Hazel"), ui->listWidget);
    QListWidgetItem *gli2 = new QListWidgetItem(tr("Hazel2"), ui->listWidget);
    ui->listWidget->addItem(gli);
    ui->listWidget->addItem(gli2);
    ui->listWidget->setSpacing(4);
    ui->listWidget->setUniformItemSizes (false);
    QImage *gi = new QImage(QString(":/images/side2.png"));
    editor = new FigureEditor();
    canvas.setSceneRect(0, 0, 512, 512);

    QAbstractGraphicsShapeItem* gsi = new ImageItem(gi);
    QAbstractGraphicsShapeItem* gsi_2 = new ImageItem(gi);
    gsi->setScale(0.2);

    //mainLayout_2->addWidget(editor, 0, 0);
    //mainLayout->addWidget(editor2, 1, 0);

    QPixmap qpi = QPixmap::fromImage (*gi);
    //qpi = qpi.scaledToHeight(50);
    canvas.addPixmap(qpi);
    QPixmap qpi_2 = qpi.copy(50,50,50,50);
    QGraphicsPixmapItem *pix = canvas.addPixmap(qpi_2);

    canvas.addItem(gsi);
    canvas.addItem(gsi_2);
    QGraphicsTextItem *ti = canvas.addText(QString(":/images/side2.png"));
    ti->setPos(50,50);
    //ui->scrollArea->setWidget(editor2);

    gsi->setPos(0,50);

    gsi->setZValue(qrand()%256);

    QObject::connect(&timer, SIGNAL(timeout()), &canvas, SLOT(advance()));
//    timer.start(30);

    QObject::connect(qApp, SIGNAL(lastWindowClosed()), qApp, SLOT(quit()) );
/*
    delete  gli;
    delete  gli2;
    delete     editor;
    delete     editor2;
    delete     gsi;
    delete     gsi_2;
    delete     gi;
*/

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::setCurrentGlWidget()
{
    currentGlWidget = qobject_cast<GLWidget *>(sender());
}

void MainWindow::rotateOneStep()
{
    if (currentGlWidget)
        currentGlWidget->rotateBy(+2 * 16, +2 * 16, -1 * 16);
}

void MainWindow::on_verticalSlider_sliderMoved(int position)
{
    rotateOneStep();
}

void MainWindow::on_pushButton_pressed()
{
    mainLayout->addWidget(glWidgets[0][0], 6, 0);
    canvas.setSceneRect(0, 0, 200, 400);
}

void MainWindow::on_pushButton_clicked()
{
    rotateOneStep();
}
